import Qs from "qs";

export const addCart = (vm, cartData) => {
  let data = {
    cartId: cartData.cartId,
    commodityId: cartData.commodityId,
    count: cartData.count,
  }
  vm.$axios.post('/cartItem/add', data)
    .then((res) => {
      vm.$message.success("添加成功")
      vm.$store.commit('setCartCount')
    })
}
export const getCartInfo = (vm) => {
  let data = Qs.stringify({
    userId: vm.$store.state.userInfo.id
  });
  vm.$axios.post('/cart/userId', data)
    .then((res) => {
      vm.$store.state.cartId = res.data.data.id;
      vm.$store.state.cartCount = res.data.data.cartItemDTOs.length
    })
}

export const getCartInfoDetail = (vm) => {
  let data = Qs.stringify({
    userId: vm.$store.state.userInfo.id
  });
  vm.$axios.post('/cart/userId', data)
    .then((res) => {
      console.log(res)
      vm.tableData = res.data.data.cartItemDTOs
      vm.isFlag = res.data.data.cartItemDTOs.length
      vm.$store.state.cartCount = res.data.data.cartItemDTOs.length
      let total = 0;
      res.data.data.cartItemDTOs.forEach((item) => {
        console.log(item)
        total += item.commodity.price * item.count
      })
      vm.totalMoney = total;
    })
}

export const deleteCartInfo = (vm,cartItemId) => {
  let data = cartItemId
  vm.$axios.delete('/cartItem/cartItemId?cartItemId='+data)
    .then((res)=>{
      getCartInfoDetail(vm)
      vm.$message.success("删除成功")
    })
}
